<?php

use com\gdl\newagk\interfaces\ResourcesServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\resources\ResourcesDTO;


class api_ResourcesService extends api_Abstract implements ResourcesServiceIf
{
	
	//获得当前组织架构
	public function attendanceGroup(ResourcesDTO $resources)
    {
		$result = new ResultDO();
		$userId =$resources->userId ? (int)$resources->userId : 0;
		$adminUserId = $resources->adminUserId ? (int)$resources->adminUserId :0;

		$result->success = true;
		$result->code = 0;
		if(!$userId || !$adminUserId){
				$result->message = '缺少参数';
				return $result;
		}

		try {
			/*******获取管理员能操作的部门**********/
			$adminuser = new model_lhbase_appadminuser();
			$userDel = $adminuser->selectOne(['id'=>$userId],'account');
			
			if(empty($userDel['account'])){
				$result->message = '管理员不存在';
				return $result;
			}
			$adminUserDel = $adminuser->selectOne(['id'=>$adminUserId],'characters,unitsId');
			
			/*******获取管理员能操作的部门 end**********/
			
			/*******获取对应考勤组**********/
			$attendancegroup = new model_attendance_attendancegroup();
			$adminuserattendancegroup = new model_attendance_adminuserattendancegroup();
			$condition= 'isDelete=0 and type=1';
			if($adminUserDel['characters']=='zg' && !$adminUserDel['unitsId']){} else {
				$userAttendancegroupId = $adminuserattendancegroup->select(['adminUserId'=>$adminUserId],'attendancegroupId')->items;
				$groupId = empty($userAttendancegroupId) ? [-1] : array_column($userAttendancegroupId,'attendancegroupId');
				$condition .= ' and id in('.implode(',',$groupId).')';
			}
			$attendanceg = $attendancegroup->select($condition,'id,title')->items;
	
            if (!empty($attendanceg)) {
			/*******获取对应管理员查看权限**********/
			$adminuserattendance = $adminuserattendancegroup->permission($userId);
			
			$result->data = $attendanceg;
			$result->datas[] = $adminuserattendance;
			$result->code = 1;
		} else {
			$result->code = 1;
			$result->message = '没有数据';
		}
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = 0;
            $result->message = '未知错误';
        }
        return $result;
	}
	
	
	//添加考勤组关联
	public function attendanceEdit(ResourcesDTO $resources)
    {
		$result = new ResultDO();
		$adminUserId = $resources->adminUserId ? (int)$resources->adminUserId :0;
		$userId = $resources->userId ? (int)$resources->userId :0;
		$resourc = $resources->filename ?  $resources->filename : [];
		$result->success = true;
		$result->code = 0;
		if(!$adminUserId || !$userId){
				$result->message = '缺少参数';
				return $result;
		}

		try {
			$adminuser = new model_lhbase_appadminuser();
			$attendancegroup = new model_attendance_attendancegroup();
			$userDel = $adminuser->selectOne(['id'=>$userId],'account');
			if(empty($userDel['account'])){
				$result->message = '管理员不存在';
				return $result;
			}
			
		//	$userUnitsId = $adminuser->operating($adminUserId);
			$adminuserattendancegroup = new model_attendance_adminuserattendancegroup();
		//	$userUnit = $attendancegroup->select('unitId in('.implode(',',$userUnitsId).')','id')->items;
			/****************/
			$adminUserDel = $adminuser->selectOne(['id'=>$adminUserId],'characters,unitsId');
			$condition= 'isDelete=0 and type=1';
			if($adminUserDel['characters']=='zg' && !$adminUserDel['unitsId']){} else {
				$userAttendancegroupId = $adminuserattendancegroup->select(['adminUserId'=>$adminUserId],'attendancegroupId')->items;
				$groupId = empty($userAttendancegroupId) ? [-1] : array_column($userAttendancegroupId,'attendancegroupId');
				$condition .= ' and id in('.implode(',',$groupId).')';
			}
			$userUnit = $attendancegroup->select($condition,'id,title')->items;
			/****************/

			$attendanUserUnits = empty($userUnit) ? [0] : array_column($userUnit,'id');
			$adminuserattendance = array_diff($resourc,$attendanUserUnits);
            if (empty($adminuserattendance)) {
			$adminuserattendancegroup->delete(['adminUserId'=>$userId]);
			foreach($resourc as $v){
				$data = [
						'adminUserId'=>$userId,
						'attendancegroupId'=>intval($v),
						];
				$adminuserattendancegroup->insert($data);
				usleep(100);
				
			}
			$result->message = '操作成功';
			$result->code = 1;
		} else {
			$result->code = 0;
			$result->message = '禁止添加不具备的资源';
		}
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = 0;
            $result->message = '未知错误';
        }
        return $result;
	}
	
}
